package com.example.web_utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.HashMap;
import java.util.Map;


/**
 * mp代码生成器
 */
public class CodeGenerator {

    private static final String PACK_PATH = "office_specifications";
    /**
     * 项目路径
     */
    private static final String PARENT_DIR = System.getProperty("user.dir");
    /**
     * 基本路径
     */
    private static final String SRC_MAIN_JAVA = "/src/main/java/";
    /**
     * xml路径
     */
    private static final String XML_PATH = PARENT_DIR + "/office-sys-web/src/main/resources/mappers";
    /**
     * entity路径
     */
    private static final String ENTITY_PATH = PARENT_DIR + "/office-sys-web/src/main/java/com/example/web/"+PACK_PATH+"/entity";
    /**
     * mapper路径
     */
    private static final String MAPPER_PATH = PARENT_DIR + "/office-sys-web/src/main/java/com/example/web/"+PACK_PATH+"/mapper";
    /**
     * service路径
     */
    private static final String SERVICE_PATH = PARENT_DIR + "/office-sys-web/src/main/java/com/example/web/"+PACK_PATH+"/service";
    /**
     * serviceImpl路径
     */
    private static final String SERVICE_IMPL_PATH = PARENT_DIR + "/office-sys-web/src/main/java/com/example/web/"+PACK_PATH+"/service/impl/";
    /**
     * controller路径
     */
    private static final String CONTROLLER_PATH = PARENT_DIR + "/office-sys-web/src/main/java/com/example/web/"+PACK_PATH+"/controller";
    /**
     * 数据库url
     */
    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/office_sys?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "123456";


    public static void main(String[] args) {
        generate();
    }

    private static Map<OutputFile, String> getPathInfo() {
        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.entity, ENTITY_PATH);
        pathInfo.put(OutputFile.mapper, MAPPER_PATH);
        pathInfo.put(OutputFile.service, SERVICE_PATH);
        pathInfo.put(OutputFile.serviceImpl, SERVICE_IMPL_PATH);
        pathInfo.put(OutputFile.controller, CONTROLLER_PATH);
        pathInfo.put(OutputFile.mapperXml, XML_PATH);
        return pathInfo;
    }


    private static void generate() {

        //获取项目路径
        String projectPath = System.getProperty("user.dir");
        //java下的文件路径
        String filePath = projectPath + "/office-sys-web/src/main/java";

        FastAutoGenerator.create(DB_URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("LS") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(filePath) // 指定输出目录
                            .disableOpenDir()// 禁止打开输出目录
                            .fileOverride(); //覆盖生成文件
                })
                .packageConfig(builder -> {
                    builder.parent("") // 设置父包名
                            .xml("mappers")
                            .entity("com.example.web."+PACK_PATH+".entity")
                            .mapper("com.example.web."+PACK_PATH+".mapper")
                            .service("com.example.web."+PACK_PATH+".service")
                            .serviceImpl("com.example.web."+PACK_PATH+".service.impl")
                            .controller("com.example.web."+PACK_PATH+".controller")
                            .pathInfo(getPathInfo());
//                            .moduleName(null) // 设置父包模块名
//                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\project\\Office_SYS\\API\\office-sys-parent\\office-sys-web\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok();
                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                    builder.addInclude("office_specifications") // 设置需要生成的表名
                            .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
                })
                .execute();

    }
}
